﻿using BLL;
using Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Printing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace UI
{
    public partial class MemberInfoForm : Form
    {
        private int _pageIndex = 1;
        private int _pageSize = 3;
        private int _pageCount = 0;
        private MemberInfoBLL _memberInfoBLL = new MemberInfoBLL();
        public MemberInfoForm()
        {
            InitializeComponent();
            comboBox1.Items.Add(3);
            comboBox1.Items.Add(5);
            comboBox1.Items.Add(7);

            comboBox1.SelectedIndex = 0;

            _pageSize = Convert.ToInt32(comboBox1.Text);

            InitMemberInfo(_pageIndex, _pageSize);
        }


        //初始化会员信息
        public void InitMemberInfo(int pageIndex, int pageSize)
        {

            //调用BLL层处理
            dgvMemmber.DataSource = _memberInfoBLL.MemberInfoSelect(pageIndex,pageSize);
            _pageCount = _memberInfoBLL.MemberCountSelect(_pageSize);

            label2.Text = _pageIndex.ToString();
            label4.Text = _pageCount.ToString();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if(_pageIndex > 1)
            {
                _pageIndex--;
                InitMemberInfo(_pageIndex, _pageSize);
            }
            
            
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (_pageIndex < _pageCount)
            {
                _pageIndex++;
                InitMemberInfo(_pageIndex, _pageSize);
            }
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            _pageSize = Convert.ToInt32(comboBox1.Text);
            InitMemberInfo(_pageIndex, _pageSize);
        }


        //添加会员
        private void btnAddMemMber_Click(object sender, EventArgs e)
        {
            AddMemberInfoForm addMemberInfoForm = new AddMemberInfoForm();
            addMemberInfoForm.ShowDialog();

            if(addMemberInfoForm.DialogResult == DialogResult.OK)
            {
                InitMemberInfo(_pageIndex, _pageSize);
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            //获取选中行的数据的id
            int id = Convert.ToInt32(dgvMemmber.SelectedRows[0].Cells[0].Value);

            //调用BLL
            if(_memberInfoBLL.DeleteMemberInfo(id) > 0)
            {
                MessageBox.Show("删除成功");
                InitMemberInfo(_pageIndex, _pageSize);
            }
            else
            {
                MessageBox.Show("删除失败");
            }
            
        }

        private void button5_Click(object sender, EventArgs e)
        {
            //获取搜索的姓名或编号
            string serch = textBox1.Text.Trim();

            //如果转化成功则查询编号，否则按姓名查找
            int val;
            bool v = int.TryParse(serch, out val);

            dgvMemmber.DataSource = _memberInfoBLL.IdOrNameSelect(val, serch, v);


        }

        //主动方发布事件
        public event EventHandler ModifyMemberInfoEvent;

        private void btnUpdateMember_Click(object sender, EventArgs e)
        {
            //获取需要修改的行数据
            MemberInfo memberInfo = new MemberInfo();

            memberInfo.Mid = Convert.ToInt32(dgvMemmber.SelectedRows[0].Cells[0].Value);
            memberInfo.Mname = dgvMemmber.SelectedRows[0].Cells[1].Value.ToString();
            memberInfo.Gender = dgvMemmber.SelectedRows[0].Cells[2].Value.ToString();
            //会员等级  
            memberInfo.Type = Convert.ToInt32(dgvMemmber.SelectedRows[0].Cells[3].Value);
            memberInfo.Discount = Convert.ToDouble(dgvMemmber.SelectedRows[0].Cells[4].Value);
            memberInfo.Money = Convert.ToDouble(dgvMemmber.SelectedRows[0].Cells[5].Value);
            memberInfo.Points = Convert.ToInt32(dgvMemmber.SelectedRows[0].Cells[6].Value);
            memberInfo.Birthday = Convert.ToDateTime(dgvMemmber.SelectedRows[0].Cells[7].Value);
            memberInfo.PhoneNum = dgvMemmber.SelectedRows[0].Cells[8].Value.ToString();
            memberInfo.RegistDate = Convert.ToDateTime(dgvMemmber.SelectedRows[0].Cells[9].Value);
            memberInfo.EndDate = Convert.ToDateTime(dgvMemmber.SelectedRows[0].Cells[10].Value);

            
            //打开修改窗体
            MemberInfoModify frm = new MemberInfoModify();
            ModifyMemberInfoEvent += frm.GetValue;

            //执行订阅的方法
            ModifyMemberInfoEvent.Invoke(this, memberInfo);

            frm.ShowDialog();

            if(frm.DialogResult == DialogResult.OK)
            {
                //初始化数据
                InitMemberInfo(_pageIndex, _pageSize);
            }
        }
    }
}
